using Newtonsoft.Json;
using PlayFab.Internal;
using PlayFab.<%- api.name %>Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace PlayFab
{
<% if(api.description) { %>
    /// <summary>
    /// <%- api.description %>
    /// </summary><% } %>
    public class PlayFab<%- api.name %>API
    {
        public PlayFabSettings Settings { get; private set; }

        public PlayFab<%- api.name %>API(PlayFabSettings settings)
        {
            Settings = settings;
        }

        public PlayFab<%- api.name %>API()
            : this(new PlayFabSettings())
        {
        }

        public PlayFab<%- api.name %>API(string titleId)
            : this(new PlayFabSettings(titleId))
        {
        }

<% if (hasServerOptions) {
%>        public PlayFab<%- api.name %>API(string titleId, string developerSecretKey)
            : this(new PlayFabSettings(titleId, developerSecretKey))
        {
        }
<% } %>

<% for(var i in api.calls) { var apiCall = api.calls[i]; %>
        <% if(apiCall.summary) {%>/// <summary>
        /// <%- GetDescriptionClean(apiCall.summary) %>
        /// </summary><%- GetReturnComment(apiCall, api) %><% } %>
<%- GetDeprecationAttribute("        ", apiCall)
%>        public async <%- GetReturnType(apiCall, api) %> <%- apiCall.name %>Async(<% if(GetCallRequestsData(apiCall, api)) { %><%- apiCall.request %> request<% } %>)
        {
<%- GetRequestActions(apiCall, api)
%>            string resultRawJson = await Settings.DoPost("<%- apiCall.name %>", <%- GetUrlAccessor(apiCall) %> + "<%- apiCall.url %>", <% if(GetCallRequestsData(apiCall, api)) { %>request<% } else {%>null<% } %>, <%- GetAuthParams(apiCall) %>);
<% if(GetCallReturnsData(apiCall, api)) { %>
            var serializer = JsonSerializer.Create(Settings.JsonSettings);
            var resultData = serializer.Deserialize<PlayFabJsonSuccess<<%- apiCall.result %>>>(new JsonTextReader(new StringReader(resultRawJson)));

            <%- apiCall.result %> result = resultData.data;<% } %>
<%- GetResultActions(apiCall, api)
%><%- GetReturnAction(apiCall, api)
%>
        }
        <% if(GetCallRequestsData(apiCall, api)) { if(apiCall.summary) {%>/// <summary>
        /// <%- GetDescriptionClean(apiCall.summary) %>
        /// </summary><%- GetParamsCommentForCall(apiCall, api) %><%- GetReturnComment(apiCall, api) %><% } %>
<%- GetDeprecationAttribute("        ", apiCall)
%>        public async <%- GetReturnType(apiCall, api) %> <%- apiCall.name %>Async(<%- JoinParamsForCall(api, apiCall, true, true, true) %>)
        {
            var request = new <%- apiCall.request %>(<%- JoinParamsForCall(api, apiCall, false, true, false) %>);
            <% if(GetCallReturnsData(apiCall, api)) { %>return <% } %>await <%- apiCall.name %>Async(request);
        }
<% } %><% } %><% if(authKey) {
%>        private string _authKey = null;

        // Determine if the _authKey is set, without actually making it public
        public bool IsClientLoggedIn()
        {
            return !string.IsNullOrEmpty(_authKey);
        }

        private async Task MultiStepClientLogin(bool needsAttribution)
        {
            if (needsAttribution && !Settings.DisableAdvertising && !string.IsNullOrEmpty(Settings.AdvertisingIdType) && !string.IsNullOrEmpty(Settings.AdvertisingIdValue))
            {
                AttributeInstallRequest request = new AttributeInstallRequest();
                if (Settings.AdvertisingIdType == PlayFabDefaultSettings.AD_TYPE_IDFA)
                    request.Idfa = Settings.AdvertisingIdValue;
                else if (Settings.AdvertisingIdType == PlayFabDefaultSettings.AD_TYPE_ANDROID_ID)
                    request.Adid = Settings.AdvertisingIdValue;
                else
                    return;
                await AttributeInstallAsync(request);
            }
        }
<% } %><% if(isAdmin) {
%>
        /// <summary>
        /// Uploads the <paramref name="build"/> to the provided <paramref name="uploadUrl"/>
        /// received by a call to GetServerBuildUploadURL(...).
        /// </summary>
        public async Task UploadBuildAsync(string uploadUrl, Stream build)
        {
            await Settings.DoPut("UploadBuild", uploadUrl, "application/x-zip-compressed", build);
        }

<% } %>    }
}
